package top.kaoshanji.p2ch24a;

import top.kaoshanji.u1constant.FileConstant;

import java.io.FileInputStream;
import java.util.Properties;

/**
 * 代码清单-p2ch24a-597页a：类加载的应用-可配置的策略
 * author kaoshanji
 * ^_^
 * create time 2025/3/3 22:34
 */
public class ConfigurableStrategyDemo {

    public static void main(String[] args) {

        ConfigurableStrategyDemo demo = new ConfigurableStrategyDemo();

        IService service = demo.createService();
        service.action();

    }

    public IService createService() {
        try {
            Properties prop = new Properties();
            String fileName = FileConstant.LOCAL_ROOT_PATH.concat("p2ch24a597页a\\config.properties");
            prop.load(new FileInputStream(fileName));

            String className = prop.getProperty("service");
            Class<?> cls = Class.forName(className);
            return (IService)cls.newInstance();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }





}
